package com.lis.springboot_lis.service;

import com.lis.springboot_lis.pojo.User;
import com.lis.springboot_lis.pojo.dto.UserDto;
import com.lis.springboot_lis.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service  //spring bean
public class UserService implements IUserService {


    @Autowired
    UserRepository userRepository;

    @Override
    public User add(UserDto user){

        User userPojo=new User();

        BeanUtils.copyProperties(user,userPojo);

        return userRepository.save(userPojo);
    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).orElseThrow(()-> new IllegalArgumentException("用户不存在，参数异常"));
    }

    @Override
    public User edit(UserDto user) {
        User userPojo=new User();
        BeanUtils.copyProperties(user,userPojo);
        return  userRepository.save(userPojo);

    }

    @Override
    public User delete(Integer userId) {
        userRepository.deleteById(userId);
        return null;
    }

}
